//
//  CustomMBHUD.m
//  XinYiGou
//
//  Created by Angel_Yan on 2017/3/30.
//  Copyright © 2017年 Angel_Yan. All rights reserved.
//

#import "CustomHUD.h"


static CustomMBHUD *HUD ;

@implementation CustomMBHUD

- (id)initWithView:(UIView *)view {
    self = [super initWithView:view];
    if (self) {
        self.userInteractionEnabled = NO;
    }
    return self;
}


+ (NSTimeInterval )timeWithTitle:(NSString *)title {
    if (title.length >= 8) {
        return title.length / 4.0;
    } else {
        return 2.0;
    }
}


// 默认的加载动画
+ (void)showHUDLoadingWithTitle:(NSString *)title;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @synchronized(HUD){
            UIView *window = kAppDelegate.window;
            if (!HUD) {
                HUD = [[CustomMBHUD alloc] initWithView:window];
                [window addSubview:HUD];
            }
            [window bringSubviewToFront:HUD];
            UIImage *image = [[UIImage imageNamed:@"Checkmarkfabu"] imageWithRenderingMode:UIImageRenderingModeAutomatic];//自定义图片
            HUD.customView = [[UIImageView alloc] initWithImage:image];
            HUD.mode =  MBProgressHUDModeIndeterminate;
            HUD.label.text= title;
            HUD.detailsLabel.text = @"";
            [HUD showAnimated:YES];
        }
    });
}


//隐藏hud
+(void)hide
{
    if (HUD) {
        [HUD hideAnimated:YES];
        [HUD removeFromSuperview];
        HUD = nil;
    }
}


// 给出提示后几秒消失
+ (void)showWithTitle:(NSString *)title
                delay:(NSTimeInterval)delay;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @synchronized(HUD){
            UIView *window = kAppDelegate.window;
            if (!HUD) {
                HUD = [[CustomMBHUD alloc] initWithView:window];
                [window addSubview:HUD];
            }
            [window bringSubviewToFront:HUD];
            UIImage *image = [[UIImage imageNamed:@"Checkmarkfabu"] imageWithRenderingMode:UIImageRenderingModeAutomatic];//自定义图片
            HUD.customView = [[UIImageView alloc] initWithImage:image];
            HUD.mode =  MBProgressHUDModeCustomView;
            HUD.detailsLabel.text = title;
            HUD.detailsLabel.font = FONT_SYS(15.0);
            [HUD showAnimated:YES];
            [HUD hideAnimated :YES afterDelay:delay];
        }
    });
}


+(void)showErrorWithStatus:(NSString *)status;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @synchronized(HUD){
            UIView *window = kAppDelegate.window;
            if (!HUD) {
                HUD = [[CustomMBHUD alloc] initWithView:window];
                [window addSubview:HUD];
            }
            [window bringSubviewToFront:HUD];
            UIImage *image = [[UIImage imageNamed:@"Checkmarkfabu"] imageWithRenderingMode:UIImageRenderingModeAutomatic];//自定义图片
            HUD.customView = [[UIImageView alloc] initWithImage:image];
            HUD.mode =  MBProgressHUDModeCustomView;
            NSString *strStatus = status;
            if ([status containsString:@"Code=-1001"]) {
                strStatus = @"请求超时";
            } else if ([status containsString:@"Code=-1009"]) {
                strStatus = @"已断开连接";
            }
            HUD.detailsLabel.text = strStatus;
            HUD.detailsLabel.font = FONT_SYS(15.0);
            [HUD showAnimated:YES];
            [HUD hideAnimated:YES afterDelay:[self timeWithTitle:status]];
        }
    });
}


+(void)showSuccessWithStatus:(NSString *)status;
{
    dispatch_async(dispatch_get_main_queue(), ^{
        @synchronized(HUD){
            UIView *window = kAppDelegate.window;
            if (!HUD) {
                HUD = [[CustomMBHUD alloc] initWithView:window];
                [window addSubview:HUD];
            }
            [window bringSubviewToFront:HUD];
            UIImage *image = [[UIImage imageNamed:@"Checkmarkfabu"] imageWithRenderingMode:UIImageRenderingModeAutomatic];//自定义图片
            HUD.customView = [[UIImageView alloc] initWithImage:image];
            HUD.mode =  MBProgressHUDModeCustomView;
            HUD.detailsLabel.text = status;
            HUD.detailsLabel.font = FONT_SYS(15.0);
            [HUD showAnimated:YES];
            [HUD hideAnimated:YES afterDelay:[self timeWithTitle:status]];
        }
    });
}



@end




